<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

function generate_cms_validation($params)
{
        echo '<pre>';
        echo "\n// codeigniter form validation";
        echo "\n\$this->form_validation\n";
        
        foreach($params as $key => $param)
        {
            $lower_readable = str_replace('-', ' ', strtolower($key));
            $lower = str_replace('_', ' ', $lower_readable);
            
            $readable_var = str_replace('-', ' ', $key);
            $readable = str_replace('_', ' ', $readable_var);

            // required validation
            $required = ( isset($param) && ! empty($param) ) ? '|required' : '';
            // max length validation
            $max_length = 255;
            // url validation
            $url = '';
            if($lower == 'url' || $lower == 'uri' || $lower == 'website') {
                $email = '|valid_url';
                $max_length = '1000';
            }
            // email validation
            $email = '';
            if($lower == 'email' || $lower == 'email address' || $lower == 'e mail' || strpos($key, 'email') ) {
                $email = '|valid_email';
                $max_length = '100';
            }
            // date validation
            $date = '';
            if($lower == 'date' || strpos($key, 'date') ) {
                $email = '|valid_slash_date';
                $max_length = '10';
            }

            end($params);
            if ( $key === key($params) )
            {
                echo ";\n\n";
            } else
            {
                echo "\n\t->set_rules('{$key}', '{$readable}', 'trim{$required}{$email}{$url}{$date}|max_length[{$max_length}]|xss_clean')";
            }
        }
        
        echo
"if (\$this->form_validation->run() == FALSE)
{
    \$response = \$this->form_validation->delimited_errors();
    \$response['status'] = 'failed';
    echo json_encode( \$response );
} else
{
    \$response = \$this->form_validation->delimited_errors();
    \$response['status'] = 'success';
    echo json_encode( \$response );
}\n\n\n"; 

        
        echo "\n// jquery form validation";
        echo "\nhttp://jqueryvalidation.org/documentation/";
        echo "\nvar rules = {};";
        
        foreach($params as $key => $param)
        {
            $lower_readable = str_replace('-', ' ', strtolower($key));
            $lower = str_replace('_', ' ', $lower_readable);
            
            $readable_var = str_replace('-', ' ', $key);
            $readable = str_replace('_', ' ', $readable_var);

            // required validation
            $required = ( isset($param) && ! empty($param) ) ? '|required' : '';
            // max length validation
            $max_length = 255;
            // url validation
            $url = '';
            if($lower == 'url' || $lower == 'uri' || $lower == 'website') {
                $email = '|valid_url';
                $max_length = '1000';
            }
            // email validation
            $email = '';
            if($lower == 'email' || $lower == 'email address' || $lower == 'e mail' || strpos($key, 'email') ) {
                $email = '|valid_email';
                $max_length = '100';
            }
            // date validation
            $date = '';
            if($lower == 'date' || strpos($key, 'date') ) {
                $email = '|valid_slash_date';
                $max_length = '10';
            }

            end($params);
            if ( $key === key($params) )
            {
                
            } else
            {
                echo "\nrules['{$key}'] = set_rules('{$key}', '{$readable}', 'trim|{$required}{$email}|max_length[{$max_length}]|xss_clean');";
            }
        }
        
        echo 
"\n\n$(\".form-validation\").validate({
    errorElement: 'small',
    errorClass: 'help-block',
    rules: rules,
    errorPlacement:function(error, element) {
        element.parents('.input-size').append( '&lt;div class=\"clear-fix\"&gt;&lt;/div&gt;' );
        element.parents('.input-size').append(error);
    },
    highlight: function(label) {
        $(label).closest('.form-group').removeClass('has-error has-success').addClass('has-error');
    },
    success: function(label) {
        label.addClass('valid').closest('.form-group').removeClass('has-error has-success').addClass('has-success');
    }
});";
}

function generate_ci_validation()
{
    $ci =& get_instance();
    
    $params = $ci->input->post();
        
        echo '<pre>';
        echo "\n// codeigniter form validation";
        echo "\n\$this->form_validation\n";
        
        foreach($params as $key => $param)
        {
            $lower_readable = str_replace('-', ' ', strtolower($key));
            $lower = str_replace('_', ' ', $lower_readable);
            
            $readable_var = str_replace('-', ' ', $key);
            $readable = str_replace('_', ' ', $readable_var);

            $required = ( isset($param) && ! empty($param) ) ? '|required' : '';
            
            $url = '';
            if($lower == 'url' || $lower == 'uri' || $lower == 'website') {
                $email = '|valid_url';
            }
            
            $email = '';
            if($lower == 'email' || $lower == 'email address' || $lower == 'e mail' || $lower == 'email add' || $lower == 'e add' ) {
                $email = '|valid_email';
            }

            end($params);
            if ( $key === key($params) )
            {
                echo ";\n\n";
            } else
            {
                echo "\n\t->set_rules('{$key}', '{$readable}', 'trim{$required}{$email}{$url}|min_length[1]|max_length[255]|xss_clean')";
            }
        }
        
        echo
"if ( \$this->form_validation->run() == FALSE )
{
    // validation error
} else
{
    // validation success
}\n\n\n"; 
        echo '</pre>';
        
        echo '<pre>';
        echo "\n// jquery form validation";
        echo "\nhttp://jqueryvalidation.org/documentation/";
        echo "\nvar rules = {};";
        
        foreach($params as $key => $param)
        {
            $lower_readable = str_replace('-', ' ', strtolower($key));
            $lower = str_replace('_', ' ', $lower_readable);
            
            $readable_var = str_replace('-', ' ', $key);
            $readable = str_replace('_', ' ', $readable_var);

            $required = ( isset($param) && ! empty($param) ) ? 'required:true, ' : '';
            
            $url = '';
            if($lower == 'url' || $lower == 'uri' || $lower == 'website') {
                $email = 'url:true, ';
            }
            
            $email = '';
            if($lower == 'email' || $lower == 'email address' || $lower == 'e mail' || $lower == 'email add' || $lower == 'e add' ) {
                $email = 'email:true, ';
            }

            end($params);
            if ( $key === key($params) )
            {
                
            } else
            {
                echo "\nrules['{$key}'] = {{$required}{$email}maxlength:255};";
            }
        }
        
        echo 
"\n\n$(\"#form-wizard\").validate({
    errorElement: 'span',
    errorClass: 'help-block',
    rules: rules,
    errorPlacement:function(error, element) {
        // add error message
        element.parent().append(error);
    },
    highlight: function(label) {
        // add error class
    },
    success: function(label) {
        // add sucess class
    }
});";
}

/* End of file service_helper.php */
/* Location: ./application/modules/backend-services/helpers/service_helper.php */